﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.Diagnostics;
using System.ServiceModel.Channels;
using log4net;


namespace ServiceOfSansar
{
    [AttributeUsage(AttributeTargets.Class)]
    public class ErrorHandler : Attribute, IErrorHandler, IServiceBehavior
    {
        static ErrorHandler()
        {
            log4net.Config.DOMConfigurator.Configure();
        }
        private static readonly ILog logger = LogManager.GetLogger("all_error");
        public bool HandleError(Exception error)
        {

            return false;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            logger.Error("->", error);
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            IErrorHandler errorHandler = new ErrorHandler();
            foreach (ChannelDispatcherBase c in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher dis = c as ChannelDispatcher;
                dis.ErrorHandlers.Add(errorHandler);
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {


        }
    }
}
